#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
const char *ps_argv[] = { "ps", "-ax", 0 };
const char *ps_envp[] = { "PATH=/bin:/usr/bin", "TERM=console", 0 };

int
main ()
{
    pid_t child,child1,child2;

    child1 = fork();
    child2 = fork();

    if(child1 < 0)
    {
        perror("child1 fork error"); 
        exit (1);
    }
    else if(child1 == 0)//在child2进程中，child1也等于0？？
    {
      if(child2 == 0)sleep(2);
        printf("in child1:execute 'ls -l',child1=%d,child2=%d\n",child1,child2); 
        if(execlp("ls", "ls", "-l",NULL) < 0)
          perror("child1 execlp error");
    }

    if(child2 < 0)
    {
        perror("child2 fork error"); 
        exit (1);
    }else if(child2 == 0)
    {
       printf("in child2,sleep5 seconds and then exit,child1=%d\n",child1);  
       sleep(5);
       exit(0);
    }
    else
    {
        printf("in parent peocess\n"); 
        do
        {
            child = waitpid(child2, NULL, WNOHANG);
            if(child == 0)
            {
                printf("the child2 has not exit\n"); 
                sleep(1);
            }
        }while(child == 0);

        if(child == child2)
          printf("Get child2\n");
        else
          printf("Error occured!\n");
    }
  //execlp ("ps", "ps", "-ax", 0);
  //if(fork() != 0)
  //{
  //  if(execl("../filter/testLinuxC", "testLinuxC", 0) < 0)
    //  printf("execl failed\n");
  //}
  //  perror("error:");
//  printf ("done!\n");
  return 0;
}
